package com.util;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "PowerFilter",value= {"/consumption/*","/home/*","/member/*","/newsManage/*",
        "/permissionsManage/*","/shop/*","/staffManage/*","/statistics/*","/top_up_rule/*","/warehouse/*",
        "/rechargereCordServlet/*","/homeServlet/*"})

public class PowerFilter implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request=(HttpServletRequest)req;
        HttpServletResponse response= (HttpServletResponse) resp;
        //判断session中是否有“staff"
        if(request.getSession().getAttribute("staff")==null){
            response.sendRedirect("/login/login.jsp");
        }else {
            //过滤链放行
            chain.doFilter(req, resp);
        }
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
